package com.zeedev.flyingsaucer.beers.activities.filter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.*;
import com.zeedev.flyingsaucer.adapter.ColumnCursorAdapter;
import com.zeedev.flyingsaucer.data.BeerFilter;
import com.zeedev.flyingsaucer.data.BeerUri;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Members;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Members.MemberBeer;
import mobi.zeedev.flyingsaucer.common.DataModule.Util;
import roboguice.activity.RoboListActivity;


public class FilterDialogActivity extends RoboListActivity {

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Variables  / State
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//
//    @Inject
//    UfoClubApp app;

    Cursor mCursor;
    ColumnCursorAdapter mAdapter;

    //	BeerFilterProvider mFilter;

    String groupBy;
    private BeerUri beerUri;

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Constants
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    //    public static final String TAG = "FilterDialogActivity";
    public static final String METABEER_GROUP_BY = "FILTER_GROUP_BY";

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Activity Lifecycle
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        app = (UfoClubApp) this.getApplication();

        this.getListView().setCacheColorHint(0);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        if (getIntent().hasExtra(BeerFilter.BEER_FILTER)) {
            beerUri = (BeerUri) getIntent().getSerializableExtra(BeerFilter.BEER_FILTER);
            groupBy = beerUri.groupBy;
        }

        mCursor = this.managedQuery(beerUri.getUri(Members.MemberBeer.FILTER_CONTENT_URI), Util.asArNonReduce(MemberBeer.STANDARD_COLUMNS, MemberBeer.MEMBER_BEER_KEY), null, null, null);


        mAdapter = new ColumnCursorAdapter(this, R.layout.my_simple_multiple_list_item, mCursor, beerUri, groupBy);


        this.setListAdapter(mAdapter);
        this.getListView().setFastScrollEnabled(true);


    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Activity Actions
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    //for api3
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //return super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //Log.d(TAG, "On back key down.");
            doReturnResults();
            return true;
        } else {
            return false;
        }

    }

//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//        doReturnResults();
//    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Menu
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.filter_dialog, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.filter_dialog_selectall:
                doSetAll(true);
                break;
            case R.id.filter_dialog_selectnone:
                doSetAll(false);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Business Logic
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    private void doReturnResults() {
        Intent intent2 = new Intent();
        //intent2.setAction(Intent.ACTION_PICK);
        //intent2.putIntegerArrayListExtra(STYLES_EXTRA, curSelected);
        intent2.putExtra(FilterDialogActivity.METABEER_GROUP_BY, groupBy);
        intent2.putExtra(BeerFilter.BEER_FILTER, beerUri);
        setResult(Activity.RESULT_OK, intent2);
        finish();
    }

    private void doSetAll(Boolean bin) {
        if (bin) {
            if (mCursor.moveToFirst()) {
                beerUri.getGroupedSelectionSet(beerUri.groupBy).clear();
                do {
                    beerUri.getGroupedSelectionSet(groupBy).add(mCursor.getString(mCursor.getColumnIndex(groupBy)));
                } while (mCursor.moveToNext());
            }
        } else {
            beerUri.getGroupedSelectionSet(groupBy).clear();
        }

        mAdapter.notifyDataSetChanged();

    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Call Backs
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

/*	private OnItemClickListener itemClicked = new OnItemClickListener() {

        @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            //Log.d(TAG,"Item clicked" + arg2);

            CheckedTextView tv = (CheckedTextView) arg1;


            Cursor curcur = (Cursor) arg0.getItemAtPosition(arg2);
            String arg3Int = curcur.getString(curcur.getColumnIndex(mFilter.getGroupBy()));
            if (tv.isChecked()) {
                mFilter.getGroupedSelectionSet().remove(arg3Int);
                //autoCheckList.remove(arg3Int);
            } else {
                mFilter.getGroupedSelectionSet().add(arg3Int);
            }

            mAdapter.notifyDataSetChanged();
        }

    };*/

}
